#include <graphics.h>
#include <conio.h>
#include<math.h>
#define High 640
#define Width 860
#define number 6

void main()
{
	float a, b;
	int i,j;
	float ball_x[number], ball_y[number];
	float ball_vx[number], ball_vy[number];
	float radius;

	initgraph(Width, High);
	for (i = 0; i < number; i++) {
		ball_x[i] = Width % number*20 + 19 * (number - 1+rand()%26);
		ball_y[i] = High % number*20 + 23 * (number - 1+rand()%24);
		ball_vx[i] = 1;
		ball_vy[i]= 1;
	}
	radius = 20;
	BeginBatchDraw();
	while (1)
	{
		for (i = 0; i < number; i++) {
			
			// 绘制黄线、绿色填充的圆
			setcolor(YELLOW);
			setfillcolor(GREEN);
			fillcircle(ball_x[i], ball_y[i], radius);			
		}
		FlushBatchDraw();
		// 延时
		Sleep(3);
		for (i = 0; i < number; i++) {
			// 绘制黑线、黑色填充的圆
			setcolor(BLACK);
			setfillcolor(BLACK);
			fillcircle(ball_x[i], ball_y[i], radius);

			ball_x[i] = ball_x[i] + ball_vx[i];
			ball_y[i] = ball_y[i] + ball_vy[i];
			if (ball_x[i] <= radius || ball_x[i] >= Width - radius) {
				ball_vx[i] = -ball_vx[i];
			}
			if (ball_y[i] <= radius || ball_y[i] >= High - radius) {
				ball_vy[i] = -ball_vy[i];
			}
			for (j = 0; j < number; j++) {
				a = fabs(ball_x[i] - ball_x[j]);
				b = fabs(ball_y[i] - ball_y[j]);
				if (a <= 2 * radius && b <= 2 * radius) {
					ball_vx[i] = -ball_vx[i];
					ball_vx[j] = -ball_vx[j];
					ball_vy[i] = -ball_vy[i];
					ball_vy[j] = -ball_vy[j];
				}
			}
		}
	}
	EndBatchDraw();
	closegraph();
}
